package com.aiyige.page.my.sell;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.WalletDetailEntity;
import com.aiyige.model.request.GetWaitReceviceMoneyDetailListRequest;
import com.aiyige.model.request.GetWalletDetailsListRequest;
import com.aiyige.model.response.WalletListResponse;
import com.aiyige.page.my.sell.adapter.WaitReceiveMoneyDetailListAdapter;
import com.aiyige.page.my.sell.model.WaitReceiveMoneyDetailResponse;
import com.aiyige.page.my.wallet.adapter.WalletDetailsListAdapter;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * 待收款明细列表
 */
@Route(path = ARouterConfig.WaitReceiveMoneyDetailListPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class WaitReceiveMoneyDetailListPage extends BaseActivity implements CommonDataView.RetrofitStub {


    @BindView(R.id.cdv)
    CommonDataView cdv;

    WaitReceiveMoneyDetailListAdapter adapter;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_price)
    TextView tvPrice;

    @Autowired
    String momentId;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_wait_receive_money_detail_list);
        ButterKnife.bind(this);
        initTitleLayout(getString(R.string.wait_receive_money_details));
        ARouter.getInstance().inject(this);
        adapter = new WaitReceiveMoneyDetailListAdapter();
        cdv.config(adapter, this);
        cdv.addItemDecoration(1, R.color.zwcolor_gray_f4f8f9);
        cdv.setRecyclerViewBgColor(ContextCompat.getColor(this, R.color.zwcolor_gray_f4f8f9));
        cdv.requestData();
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return GetWaitReceviceMoneyDetailListRequest.newBuilder()
                .momentId(momentId)
                .pageNum(currentPage + "")
                .build();
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            WaitReceiveMoneyDetailResponse response = JsonUtil.toObject(respJson, WaitReceiveMoneyDetailResponse.class);
            if (response != null && response.getStatistics() != null) {
                tvTitle.setText(response.getStatistics().getTitle());
                tvPrice.setText(StringUtils.priceFormat(response.getStatistics().getAddupWaitPay()));
            }
            if (response != null && !ListUtil.isEmpty(response.getContent())) {
                resultList.addAll(response.getContent());
                return new CommonDataView.HandleResult(response.isLast());
            }
            return new CommonDataView.HandleResult(true);
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }


}
